package qjc.z_排序;

import java.util.Arrays;

public class z2_选择排序 {
    /**
     * 每一轮选择最大的元素交换到合适的位置
     *
     * @param nums
     */
    private static void sort(int[] nums) {
        int max = 0;
        for (int i = nums.length - 1; i > 0; i--) {
            max = i;
            for (int j = 0; j < i; j++) {
                if (nums[j] > nums[max]) {
                    max = j;
                }
            }
            if (max != i) {
                swap(nums, i, max);
            }
        }
    }

    private static void swap(int[] nums, int i, int max) {
        int temp = nums[i];
        nums[i] = nums[max];
        nums[max] = temp;
    }

    public static void main(String[] args) {
        int[] a = {6, 5, 4, 3, 2, 1};
        System.out.println(Arrays.toString(a));
        sort(a);
        System.out.println(Arrays.toString(a));
    }
}
